#include<stdio.h>
#include<stdlib.h>
#include<strings.h>

#include "tree.h"



void init(tree* t) {
	*t = NULL;
}

int destroy_t(tree* tr) {
	
	tree t = *tr;
	
	if (t == NULL) { return 1;}
	
	if(t->left == NULL && t->right == NULL) {
		free(t->item);
		return 1;		
	}
	
	if(t->left != NULL && t->right == NULL) {
		destroy_t(&(t->left));		
		free(t->item);
		return 1;
	}
	
	if(t->left == NULL && t->right != NULL) {
		destroy_t(&(t->right));		
		free(t->item);		
		return 1;
	}
	
	if(t->left != NULL && t->right != NULL) {
		destroy_t(&(t->left));		
		destroy_t(&(t->right));		
		free(t->item);		
		return 1;
	}
	
	
	
	
	
}


int isEmpty(tree t) {
	return (t==NULL);
}

int insert(tree* t, char* v) {
	
	if (isEmpty(*t)) {
		//nodo vuoto
		(*t) = (tree) malloc(sizeof(struct node));
		(*t)-> item = (char*) malloc(MAX_CHAR * sizeof(char));
		if ( ( (*t) == NULL ) || (  (*t)->item == NULL )) {
			printf("ERRORE: allocazione memoria non riuscita");
			return 0;
		}
		
		strcpy((*t)-> item, v);
		(*t)->count = 1;
		(*t)->left = NULL;
		(*t)->right = NULL;	
		
		return 1;
		
	} else {
		//nodo gia esistente
		int valconf = strcmp(v, (*t)->item);
		
		if (valconf == 0){
			(*t)->count++;
			return 1;}
		if(valconf < 0) {
			insert( &(*t)->left , v);
		}
		if (valconf > 0) {
			insert( &(*t)->right , v);
			
			
		}
		
		
		
		
	}
	
	
	
	
	
}

int cerca(tree t, char* v) {
	
	
	
}




int print_pre(tree t){
	
	if (t == NULL) { return 1;}
	else {
		printf("%s\n", t->item);
		print_pre( t->left);
		print_pre( t->right);
	}
}

int print_post(tree t){
	if (t == NULL) { return 1;}
	else {
		print_post( t->left);
		print_post( t->right);
		printf("%s", t->item);
		printf("%s", "   ");
		printf("%i\n", t->count);
	}
}
int print_in(tree t){
	if (t == NULL) { return 1;}
	else {
		print_in( t->left);
		printf("%s\n", t->item);
		print_in( t->right);
	}
	
	
}
int print_rev(tree t){
	if (t == NULL) { return 1;}
	else {
		print_rev( t->right);
		printf("%s\n", t->item);
		print_rev( t->left);
	}
}


int cverb(char* parola) {
	
	int len = strlen(parola);
	
	char tu, pu, u;
	
	tu = parola[len - 3];
	pu = parola[len - 2];
	u = parola[len - 1];
	
	if ( (tu == 'a' || tu == 'e' || tu == 'i') && pu == 'r' && u == 'e'    ) {return 1;}
	else {return 0;}
	
	
}

///Rodrigo Sestari
int insertOrderParole(tree* t,int vcount, char* v) {
	
	if (isEmpty(*t)) {
		(*t) = (tree) malloc(sizeof(struct node));
		(*t)-> item = (char*) malloc(MAX_CHAR * sizeof(char));
		
		if ( ( (*t) == NULL ) || (  (*t)->item == NULL )) {			
			printf("ERRORE: allocazione memoria non riuscita");			
			return 0;			
		}		
		
		strcpy((*t)-> item, v);		
		(*t)->count = vcount ;		
		(*t)->left = NULL;		
		(*t)->right = NULL;
		return 1;
		
	} else {
		
		if (vcount == (*t)->count){
			char* text = (*t)->item;
			(*t)-> item = (char*) malloc(MAX_CHAR * (sizeof((*t)->item) + sizeof(char)));
			strcpy ((*t)->item,text);
			strcat ((*t)->item,",");
			strcat ((*t)->item,v);         								
			free(text);
			return 1;}
		
		if(vcount < (*t)->count) {			
			insertOrderParole( &(*t)->left ,vcount, v);			
		}
		
		if (vcount > (*t)->count) {		
			insertOrderParole( &(*t)->right ,vcount, v);		
		}
		
	}
	
}


int print_insert_OrderParole(tree tvechio,tree* tnouvo){
	
	if (tvechio == NULL) { return 1;}
	
	else {
		insertOrderParole(&(*tnouvo),tvechio->count,tvechio->item);		
		print_insert_OrderParole( tvechio->left, &(*tnouvo));		
		print_insert_OrderParole( tvechio->right,&(*tnouvo));			
	}
}





